package longrise.phone.com.bjjt_henan.bindinginformation;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.LFView;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.util.UiUtil;

import longrise.phone.com.bjjt_henan.Adapter.PictureAdapter;
import longrise.phone.com.bjjt_henan.MainActivity;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.ApiImpI;
import longrise.phone.com.bjjt_henan.Utils.CommontUtils;
import longrise.phone.com.bjjt_henan.Utils.DialogUtils;

/**
 * 随手拍详情
 */

public class SnapshotDetailView extends LFView {

    private Context mContext;

    private View mView;

    private EntityBean baseBean;

    private BaseApplication mApplication;

    private PictureAdapter mAdapter;

    private TextView carTypeTV;

    private TextView carnumTV;

    private TextView resultTV;

    private GridView picGV;

    public SnapshotDetailView(Context context, EntityBean bean) {
        super(context);
        this.mContext = context;
        this.baseBean = bean;
        this.mApplication = ((MainActivity) context).getAppData();
    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter form = new FormParameter();
        form.setShowtitle(false);
        return form;
    }

    @Override
    public View getView() {
        return mView;
    }

    @Override
    public void init() {
        mView = LayoutInflater.from(mContext).inflate(R.layout.view_snapshot_detail, null);
        if (null == mView) {
            return;
        }
        //标题栏
        TextView titleTV = (TextView) mView.findViewById(R.id.tv_title);
        titleTV.setText("拍摄详情");
        mView.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnDestroy();
                closeForm();
            }
        });

        TextView timeTV = (TextView) mView.findViewById(R.id.detail_tv_time);
        TextView typeTV = (TextView) mView.findViewById(R.id.detail_tv_type);
        TextView addressTV = (TextView) mView.findViewById(R.id.detail_tv_address);
        picGV = (GridView) mView.findViewById(R.id.detail_gv);
        carTypeTV = (TextView) mView.findViewById(R.id.detail_tv_cartype);
        carnumTV = (TextView) mView.findViewById(R.id.detail_tv_carnum);
        resultTV = (TextView) mView.findViewById(R.id.detail_tv_result);
        Button backBTn = (Button) mView.findViewById(R.id.detail_btn_back);
        backBTn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnDestroy();
                closeForm();
            }
        });

        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
        float screenWidth = dm.widthPixels;
        mAdapter = new PictureAdapter(mContext, screenWidth);
        mAdapter.setType(1);
        picGV.setAdapter(mAdapter);

        timeTV.setText(baseBean.getString("createtime"));
        typeTV.setText(CommontUtils.getPeccancyStr(baseBean.getString("accidenttype")));
        addressTV.setText(baseBean.getString("accidentplace"));
    }

    @Override
    public void refresh() {
        String uid = mApplication.getKckpName();
        String token = mApplication.getToken();
        String caseno = baseBean.getString("appcaseno");
        DialogUtils.showDialog(mContext);
        ApiImpI.getInstance(mContext.getApplicationContext()).snapshotDetails(caseno, uid, token, new ApiImpI.RequestCallBack() {
            @Override
            public void onError(String error) {
                DialogUtils.closeDialog();
                if (!TextUtils.isEmpty(error)) {
                    UiUtil.showToast(mContext, error);
                }
            }

            @Override
            public void onSuccess(Object result) {
                DialogUtils.closeDialog();
                if (mView == null) {
                    return;
                }
                EntityBean bean = (EntityBean) result;
                String restate = bean.getString("restate");
                if (TextUtils.equals(restate, "1")) {
                    EntityBean dataBean = bean.getBean("data");
                    String cartype = dataBean.getString("carnumkind");
                    if (TextUtils.equals(cartype, "1")) {
                        carTypeTV.setText("小型车辆");
                    } else if (TextUtils.equals(cartype, "2")) {
                        carTypeTV.setText("大型车辆");
                    }
                    String carno = dataBean.getString("casecarno");
                    if (!TextUtils.isEmpty(carno)) {
                        carnumTV.setText(carno);
                    }
                    EntityBean[] pics = dataBean.getBeans("passlist");
                    if (null != pics) {
                        for (int i = 0; i < pics.length; i++) {
                            mAdapter.addData(pics[i]);
                        }
                        mAdapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        if (null != picGV) {
            picGV.setAdapter(null);
            picGV = null;
        }
        mView = null;
    }
}
